package com.jd.finance.core.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jd.finance.core.mapper.ConnectMapper;
import com.jd.finance.core.mapper.LoanApplyMapper;
import com.jd.finance.core.pojo.entity.Connect;
import com.jd.finance.core.pojo.entity.LoanApply;
import com.jd.finance.core.service.ConnectService;
import com.jd.finance.core.service.UserService;
import com.jd.finance.result.BaseResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 用户离职 交接申请表 服务实现类
 * </p>
 *
 * @author LiQingBo
 * @since 2022-05-25
 */
@Service
public class ConnectServiceImpl extends ServiceImpl<ConnectMapper, Connect> implements ConnectService {

    @Resource
    private ConnectMapper connectMapper;

    @Resource
    private UserService userService;
    @Resource
    private LoanApplyMapper loanApplyMapper;

    /**
     * 发起交接申请
     *
     * @param uid     管理员id
     * @param leaveId 离职人人id
     * @param onId    在职人id
     * @return
     */
    @Override
    public BaseResult applyConn(Integer uid, Integer leaveId, Integer onId, String loanApplyId) {
        if (uid == null) {
            log.error("uid为空");
            return BaseResult.error().message("uid为空");
        }
        if (leaveId == null) {
            log.error("离职人员id为空");
            return BaseResult.error().message("离职人员id为空");
        }
        if (onId == null) {
            log.error("在职人员id为空");
            return BaseResult.error().message("在职人员id为空");
        }
        if (loanApplyId == null) {
            log.error("负责的业务id为空");
            return BaseResult.error().message("负责的业务id为空");
        }
        Connect connect = new Connect();
        connect.setLeaveId(leaveId);
        connect.setOnId(onId);
        connect.setLoanApplyId(loanApplyId);
        int i = connectMapper.insertConnect(leaveId, onId, loanApplyId);
        if (i == 0) {
            log.error("发起申请失败");
            return BaseResult.error().message("发起申请失败");
        }
        return BaseResult.ok();
    }

    /**
     * 发起审核交接申请
     *
     * @param uid
     * @param connectId 交接id
     * @param isPass    审核状态
     * @return
     */
    @Override
    public BaseResult examConnectApply(Integer uid, Integer connectId, Integer isPass) {
        if (uid == null) {
            log.error("uid为空");
            return BaseResult.error().message("uid为空");
        }
        if (connectId == null) {
            log.error("connectId为空");
            return BaseResult.error().message("connectId为空");
        }
        if (isPass == null) {
            log.error("输入的审核状态为空");
            return BaseResult.error().message("输入的审核状态为空");
        }

        Connect connect = new Connect();
        connect.setConnectId(connectId);
        connect.setIsPass(isPass);

        int i = connectMapper.updateById(connect);
        if (i >= 1) {
            return BaseResult.ok().data("shenhezhuangtai", i);
        } else {
            return BaseResult.ok().data("shenhezhuangtai", 0);
        }
    }

    /**
     * 业务交接
     *
     * @param uid
     * @param leaveId
     * @param onId
     * @param loanApplyId
     * @return
     */
    @Override
    public BaseResult examConnect(Integer uid, Integer leaveId, Integer onId, String loanApplyId) {
        if (uid == null) {
            log.error("uid为空");
            return BaseResult.error().message("uid为空");
        }
        if (leaveId == null) {
            log.error("离职人员id为空");
            return BaseResult.error().message("离职人员id为空");
        }
        if (onId == null) {
            log.error("在职人员id为空");
            return BaseResult.error().message("在职人员id为空");
        }
        if (loanApplyId == null) {
            log.error("负责的业务id为空");
            return BaseResult.error().message("负责的业务id为空");
        }

        //用逗号拆分传过来的数据，遍历修改数据
        int i = 0;
        String[] split = loanApplyId.split(",");
        int[] ints = new int[split.length];
        for (String s : split) {
            LoanApply loanApply = new LoanApply();
            loanApply.setLoanAppId(Integer.parseInt(s));
            loanApply.setWorkerId(onId);
            ints[i++] = loanApplyMapper.updateById(loanApply);
        }

        //遍历结果，如果有0就返回失败，全为1就返回成功
        int update = 0;
        for (int j = 0; j < ints.length; j++) {
            update = 1;
            if (ints[j] == 0) {
                update = 0;
                break;
            }
        }
        if (update >= 1) {
            return BaseResult.ok().data("jiaojiejieguo", update);
        } else {
            return BaseResult.ok().data("jiaojiejieguo", 0);
        }
    }
}
